package com.jiandan.business.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jiandan.base.entity.ExtData;
import com.jiandan.base.mapper.BeanMapper;
import com.jiandan.base.utils.ExtDataUtil;
import com.jiandan.business.entity.Cemetery;
import com.jiandan.business.entity.CemeteryArea;
import com.jiandan.business.repository.CemeteryAreaDao;
import com.jiandan.business.repository.CemeteryAreaMapperDao;
import com.jiandan.business.vo.CemeteryAreaVo;
import com.jiandan.business.vo.CemeterySimpleVo;


@Service
@Transactional(readOnly=true)
public class CemeteryAreaServiceImpl implements CemeteryAreaService{
	
	@Autowired
	private CemeteryAreaDao areaDao;
	
	@Autowired
	private CemeteryAreaMapperDao areaMapperDao;
	
	/**
	 * 获取墓区信息.
	 *
	 * @param areaId the area id
	 * @return the area detail info
	 */
	public CemeteryAreaVo getAreaDetailInfo(Long areaId){
		CemeteryAreaVo areaVo = new CemeteryAreaVo();
		List<CemeterySimpleVo> cemeterySimpleVos = new ArrayList<CemeterySimpleVo>();
		CemeteryArea area = areaDao.findOne(areaId);
		BeanMapper.copy(area, areaVo);
		Long maxRowNum = areaMapperDao.getMaxRowNum(areaId);
		Long maxColNum = areaMapperDao.getMaxColNum(areaId);
		areaVo.setMaxRowNum(maxRowNum);
		areaVo.setMaxColNum(maxColNum);
		Set<Cemetery> cemeteries = area.getCemeteries();
		for (Cemetery cemetery : cemeteries) {
			CemeterySimpleVo simpleVo = new CemeterySimpleVo();
//			String rowNumFormat = CemeteryUtil.numberFormat(cemetery.getRowNum(), "000");
//			String colNumFormat = CemeteryUtil.numberFormat(cemetery.getColNum(), "000");
			simpleVo.setId(cemetery.getId());
			simpleVo.setName(cemetery.getRowNum() + "--" + cemetery.getColNum());
			simpleVo.setRowNum(maxRowNum - cemetery.getRowNum() + 1L);
			simpleVo.setColNum(maxColNum - cemetery.getColNum() + 1L);
			simpleVo.setStatus(cemetery.getStatus());
			cemeterySimpleVos.add(simpleVo);
		}
		areaVo.setCemeteryNum(Long.valueOf(cemeteries.size()));
		areaVo.setCemeteries(cemeterySimpleVos);
		return areaVo;
	}

}
